<?php
global $_GPC, $_W;
function getcode($code)
{
    $cityinfo = pdo_get('yzzc_sun_city', array('code' => $code));
    return $cityinfo;
}

$GLOBALS['frames'] = $this->getMainMenu();
$platform_fee = pdo_get('yzzc_sun_store_config', ['uniacid' => $_W['uniacid']], 'platform_fee');

$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where type = 2 and uniacid =" . $_W['uniacid'];
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and name LIKE  '%$op%' or mobile like '%$op%'";
    }
    if ($_GPC['type'] == 'wait') {
        $where .= " and status =1";
    } elseif ($_GPC['type'] == 'ok') {
        $where .= " and status =2";
    }
    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_store') . "{$where} ORDER BY store_id DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    foreach ($info as $key => $value) {
        $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
        $province = getcode($value['province']);
        $info[$key]['province'] = $province['fullname'] ?? '';

        $city = getcode($value['city']);
        $info[$key]['city'] = $city['fullname'] ?? '';

        $area = getcode($value['area']);
        $info[$key]['area'] = $area['fullname'] ?? '';
    }
    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_store") . $where);
    $pager = pagination($total, $page, $size);
    include $this->template('web/store_personal/store_list');
} elseif ($op == 'add') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    if (checksubmit('submit')) {
        if (empty($_GPC['uid'])) {
            message('会员ID不能为空', '', 'error');
        }

        if (!$user = pdo_get('yzzc_sun_user', ['id' => $_GPC['uid'], 'uniacid' => $_W['uniacid']])) {
            message('会员尚未注册', '', 'error');
        }

        $data = [
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'] ?? $user['user_name'],
            'mobile' => $_GPC['mobile'] ?? $user['user_tel'],
            'province' => $_GPC['province'],
            'city' => $_GPC['city'],
            'area' => $_GPC['area'],
            'address' => $_GPC['address'],
            'lng' => $_GPC['lng'],
            'lat' => $_GPC['lat'],
            'id_card_img_front' => $_GPC['id_card_img_front'],
            'id_card_img_side' => $_GPC['id_card_img_side'],
            'pay_status' => 2,
            'createtime' => time()
        ];

        $store = pdo_get('yzzc_sun_store', ['uid' => $_GPC['uid'], 'uniacid' => $_W['uniacid']]);
        if ($store) {
            message('请勿重复申请', $this->createWebUrl('store_personal', array()), 'error');
        }

        $result = pdo_insert('yzzc_sun_store', $data);
        if ($result) {
            message('申请成功，等待审核', $this->createWebUrl('store_personal', array()), 'success');
        } else {
            message('申请失败', $this->createWebUrl('store_personal', array()), 'error');
        }
    }
    include $this->template('web/store/store_add');
} elseif ($op == 'edit') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    $info = pdo_get('yzzc_sun_store', ['uniacid' => $_W['uniacid'], 'store_id' => $_GPC['store_id']]);
    $info['province_name'] = getcode($info['province'])['fullname'];
    $info['city_name'] = getcode($info['city'])['fullname'];
    $info['area_name'] = getcode($info['area'])['fullname'];
    $info['pic'] = $info['pic'] ? explode(',', $info['pic']) : '';
    if (checksubmit('submit')) {
        $data = [
            'uniacid' => $_W['uniacid'],
            'name' => trim($_GPC['name']),
            'mobile' => trim($_GPC['mobile']),
            'province' => trim($_GPC['province']),
            'city' => trim($_GPC['city']),
            'area' => trim($_GPC['area']),
            'lng' => trim($_GPC['lng']),
            'lat' => trim($_GPC['lat']),
            'address' => trim($_GPC['address']),
            'driving_license' => trim($_GPC['driving_license']),
            'id_license' => trim($_GPC['id_license']),
            'sy_insurance' => trim($_GPC['sy_insurance']),
            'jq_insurance' => trim($_GPC['jq_insurance']),
            'updatetime' => time(),
        ];
        $result = pdo_update('yzzc_sun_store', $data, [
            'store_id' => $_GPC['store_id'],
            'uniacid' => $_W['uniacid']
        ]);
        if ($result) {
            message('修改成功', $this->createWebUrl('store_personal', array('op' => 'display')), 'success');
        } else {
            message('修改失败', $this->createWebUrl('store_personal', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
        }
    }
    include $this->template('web/store_personal/store_personal_edit');
} elseif ($op == 'delete') {
    $store_info = pdo_get('yzzc_sun_store', array('store_id' => $_GPC['id']));
    $branch = pdo_get('yzzc_sun_branch', array('store_id' => $_GPC['id']));
    if ($store_info['status'] == 1) {
        $res1 = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        $res2 = pdo_delete('yzzc_sun_branch', array('store_id' => $_GPC['id']));
        $res3 = pdo_delete('yzzc_sun_branch_admin', array('bid' => $branch['id']));
        if ($res1 && $res3 && $res2) {
            message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    } else {
        $res = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        if ($res) {
            message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
} elseif ($op == 'disable') {
    $res = pdo_update('yzzc_sun_store', array('is_disable' => $_GPC['is_disable']), ['store_id' => $_GPC['id']]);
    if ($res) {
        message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }

} elseif ($op == 'change') {
    $where = ['store_id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_store', $where, ['uid', 'uniacid']);
    $data = [];
    if (isset($_GPC['platform_fee']) && $_GPC['platform_fee']) {
        $data['platform_fee'] = $_GPC['platform_fee'];
    }
    if (isset($_GPC['payout_fee']) && $_GPC['payout_fee']) {
        $data['payout_fee'] = $_GPC['payout_fee'];
    }
    $data['updatetime'] = time();
    $res = pdo_update('yzzc_sun_store', $data, $where);
    if ($res) {
        message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
} elseif ($op == 'income') {
    $store_id = $_GPC['store_id'];

    $branch_list = pdo_getall('yzzc_sun_branch', ['uniacid' => $_W['uniacid']], ['id', 'name']);
    $where = "where i.uniacid =" . $_W['uniacid'];
    if ($store_id) {
        $where .= ' and i.store_id=' . $store_id;
    }
    $keywords = $_GPC['keywords'] ?? '';
    if ($keywords) {
        $where .= " and (o.username LIKE  '%$keywords%' or o.tel LIKE  '%$keywords%' or o.ordernum LIKE  '%$keywords%')";
    }
    $branch_id = $_GPC['branch_id'] ?? '';
    if ($branch_id) {
        $where .= " and i.branch_id=" . $branch_id;
    }

    if (isset($_GPC['status']) && $_GPC['status'] != '') {
        $where .= ' and i.status=' . $_GPC['status'];
    }

    $type = $_GPC['type'] ?? '';
    if ($type) {
        $where .= ' and i.type=' . $type;
    }
    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT i.id,i.total,i.type,i.platform_fee,i.branch_id,i.amount,i.status,i.createtime,i.income_time,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_branch_income') . " i left join " . tablename('yzzc_sun_order') . " o on i.order_id=o.id {$where} ORDER BY i.createtime DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    $search_total = $wait_amount = $finish_amount = 0;

    $store_name = pdo_fetchcolumn('select name from '.tablename('yzzc_sun_store').' where store_id = '.$store_id);

    if ($info) {
        foreach ($info as $key => $value) {
            foreach ($branch_list as $val) {
                if ($value['branch_id'] == $val['id']) {
                    $info[$key]['branch_name'] = $val['name'];
                }
            }
            $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
            $info[$key]['income_time'] = $value['income_time'] ? date('Y-m-d H:i:s', $value['income_time']) : '';
        }

        //统计
        $sql = 'SELECT i.id,i.total,i.type,i.platform_fee,i.branch_id,i.amount,i.status,i.createtime,i.income_time,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_branch_income') . " i left join " . tablename('yzzc_sun_order') . " o on i.order_id=o.id {$where}";
        $total_count = pdo_fetchall($sql);
        foreach ($total_count as $value) {
            if ($value['status'] == 0) {
                $wait_amount += $value['amount'];
            }
            if ($value['status'] == 1) {
                $finish_amount += $value['amount'];
            }
        }
    }
    return include $this->template('web/store_personal/income_list');
}
